package cn.elwy.rcp.kit.intro.handler;

import java.util.Dictionary;

import org.eclipse.core.runtime.IProduct;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.resource.StringConverter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.branding.IProductConstants;
import org.eclipse.ui.splash.BasicSplashHandler;
import org.eclipse.wb.swt.SWTResourceManager;

import cn.elwy.rcp.kit.Activator;

/**
 * 自定义Splash，带登录功能，登录成功后显示进度条
 * @author huangsq
 * @version 1.0, 2014-01-07
 * @since 1.0, 2014-01-07
 */
public class SplashHandler extends BasicSplashHandler {

    private final int LOGIN_COMPOSITE_WIDTH = 260;
    private final int LOGIN_COMPOSITE_HEIGHT = 100;
    private final int MARGIN_WIDTH = 5;
    private final int MARGIN_HEIGHT = 0;
    private Rectangle progressRect;
    private Rectangle messageRect;
    private int rgb[];
    private IProduct product;
    private Color foreground;

    public SplashHandler() {
    }

    public void init(Shell splash) {
        super.init(splash);

        String progressRectString = null;
        String messageRectString = null;
        String foregroundColorString = null;
        product = Platform.getProduct();
        if (product != null) {
            progressRectString = product.getProperty(IProductConstants.STARTUP_PROGRESS_RECT);
            messageRectString = product.getProperty(IProductConstants.STARTUP_MESSAGE_RECT);
            foregroundColorString = product.getProperty(IProductConstants.STARTUP_FOREGROUND_COLOR);
        }
        progressRect = StringConverter.asRectangle(progressRectString, new Rectangle(10, 10, 300, 15));
        messageRect = StringConverter.asRectangle(messageRectString, new Rectangle(10, 35, 300, 15));

        int foregroundColor;
        try {
            foregroundColor = Integer.parseInt(foregroundColorString, 16);
        } catch (Exception ex) {
            foregroundColor = 0xD2D7FF; // off white
        }

        rgb = new int[] { (foregroundColor & 0xFF0000) >> 16, (foregroundColor & 0xFF00) >> 8, foregroundColor & 0xFF };
        foreground = SWTResourceManager.getColor(rgb[0], rgb[1], rgb[2]);

        splash.setBackgroundMode(SWT.INHERIT_FORCE);
        splash.setForeground(foreground);

        createLoginComposite();
        createContent();
    }

    /**
     * 创建登录面板
     */
    private void createLoginComposite() {
        Color background = SWTResourceManager.getColor(SWT.COLOR_WHITE);
        LoginComposite loginComposite = new LoginComposite(getSplash(), SWT.NONE);

        Control[] children = loginComposite.getChildren();
        for (Control control : children) {
            if (control instanceof Label) {
                control.setForeground(foreground);
            } else if (control instanceof Text) {
                control.setBackground(background);
            }
        }

        int x = getSplash().getSize().x - LOGIN_COMPOSITE_WIDTH - MARGIN_WIDTH;
        int y = getSplash().getSize().y - LOGIN_COMPOSITE_HEIGHT - MARGIN_HEIGHT;
        loginComposite.setBounds(x, y, LOGIN_COMPOSITE_WIDTH, LOGIN_COMPOSITE_HEIGHT);
        loginComposite.doEventLoop();
        loginComposite.dispose();
    }

    /**
     * 创建进度条及状态信息
     */
    private void createContent() {
        setProgressRect(progressRect);
        setMessageRect(messageRect);
        setForeground(new RGB(rgb[0], rgb[1], rgb[2]));

        Dictionary<String, String> headers = Activator.getDefault().getBundle().getHeaders();
        final String buildId = product.getName() + "V " + headers.get("Bundle-Version");
        String buildIdLocString = product.getProperty("buildIdLocation"); //$NON-NLS-1$
        final Point buildIdPoint = StringConverter.asPoint(buildIdLocString, new Point(260, 200));
        final String copyright = "Copyright (c) 2012-2020 elwy , Inc. All rights reserved.";
        getContent().addPaintListener(new PaintListener() {

            public void paintControl(PaintEvent e) {
                e.gc.setForeground(getForeground());
                e.gc.drawText(buildId, buildIdPoint.x, buildIdPoint.y, true);
                Point splashPoint = SplashHandler.this.getSplash().getSize();
                e.gc.drawText(copyright, 10, splashPoint.y - 25, true);
            }
        });
    }

}
